package aar.test.tessocr;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import aar.test.tessocr.EventMessage.EventMessage;
import aar.test.tessocr.EventMessage.MessageEvent;
import aar.test.tessocr.base.BaseActivity;
import aar.test.tessocr.baseApplication.BaseApplication;
import aar.test.tessocr.db.impl.ClientInfoDaoImp;
import aar.test.tessocr.model.DialogCallback;
import aar.test.tessocr.model.LzyResponse;
import aar.test.tessocr.utils.PrefUtils;
import aar.test.tessocr.utils.Urls;
import aar.test.tessocr.utils.Utils;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import clx.tessocr.ocreng.R;
import okgo.OkGo;
import okgo.model.Response;
import okhttp3.RequestBody;

import static aar.test.tessocr.EventMessage.MessageEvent.SEARCH_CABINET;
import static aar.test.tessocr.Login1Activity.JSON;

/**
 * Created by sunshine on 2020/10/14
 */
public class OpenAndDeliverActivity extends BaseActivity {
    @BindView(R.id.txt_num)
    TextView txtNum;
    @BindView(R.id.box_type)
    TextView boxType;
    @BindView(R.id.deliver_num)
    TextView deliverNum;
    @BindView(R.id.tel_num)
    TextView telNum;
    @BindView(R.id.iv_phone_status)
    ImageView iv_phone_status;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_open_and_deliver);
        ButterKnife.bind(this);
        initView();
        /*if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }*/
    }

    private void initView() {
        txtNum.setText(BaseApplication.getIntance().getmSiteDispatchModel().getGridnum() + "号");
        deliverNum.setText(BaseApplication.getIntance().getmSiteDispatchModel().getExpressNum());
        boxType.setText(BaseApplication.getIntance().getmSiteDispatchModel().getGridtypeStr());
        String phone = BaseApplication.getIntance().getmSiteDispatchModel().getTel();
        telNum.setText(phone);
        if (ClientInfoDaoImp.getInstance(OpenAndDeliverActivity.this).hasOldTelNmu(phone)) {
            iv_phone_status.setImageResource(R.mipmap.img_old);
            telNum.setTextColor(getResources().getColor(R.color.text_green));
        } else {
            iv_phone_status.setImageResource(R.mipmap.img_new);
            telNum.setTextColor(getResources().getColor(R.color.text_red));
        }
    }

    @OnClick({R.id.left_layout, R.id.txt_rechoose, R.id.txt_modify, R.id.tv_goon, R.id.tv_done})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.left_layout:
                finish();
                break;
            case R.id.txt_rechoose:
                finish();
                break;
            case R.id.txt_modify:

                break;
            case R.id.tv_goon:
                EventBus.getDefault().post(new EventMessage().setMessage("DeliverCancel"));
                finish();
                break;
            case R.id.tv_done:
                gridConfirem();
                break;
        }
    }

    private void gridConfirem() {
        if (TextUtils.isEmpty(PrefUtils.getString(this, "userId", ""))) {
            Toast.makeText(OpenAndDeliverActivity.this, "管理员id为空", Toast.LENGTH_SHORT).show();
            return;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("cabinetGridId", BaseApplication.getIntance().getmSiteDispatchModel().getCabinetgridid());
        map.put("express", BaseApplication.getIntance().getmSiteDispatchModel().getExpressNum());
        map.put("phone", BaseApplication.getIntance().getmSiteDispatchModel().getTel());
        map.put("adminid", PrefUtils.getString(this, "userId", ""));

        Gson gg = new Gson();
        String json = gg.toJson(map);

        RequestBody body = RequestBody.create(JSON, json);
        OkGo.<LzyResponse>post(Urls.URL_XIANCHANG_CONFIRM)//
                .tag(this)//
                .upRequestBody(body)
                .isMultipart(false)         //强制使用 multipart/form-data 表单上传（只是演示，不需要的话不要设置。默认就是false）
                .execute(new DialogCallback<LzyResponse>(OpenAndDeliverActivity.this) {
                    @Override
                    public void onSuccess(Response<LzyResponse> response) {


                        Log.e("MainActivity", "onSuccess-----logout---------------" + response.body().data);
                        if (null != response && null != response.body() && null != response.body().msg) {
                            Toast.makeText(getApplicationContext(), response.body().msg, Toast.LENGTH_SHORT).show();
                            ClientInfoDaoImp.getInstance(OpenAndDeliverActivity.this).AddTelNum(BaseApplication.getIntance().getmSiteDispatchModel().getTel());
                            BaseApplication.getIntance().setmSiteDispatchModel(null);
                            EventBus.getDefault().post(new EventMessage().setMessage("DeliverDone"));
                            finish();
                        }
                    }

                    @Override
                    public void onError(Response<LzyResponse> response) {

                        Log.e("MainActivity", "onError---------------------");
                        if (null != response && null != response.body() && null != response.body().msg) {
                            Toast.makeText(getApplicationContext(), response.body().msg, Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    /*@Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);

    }*/
}
